package com.cmccsi.service.impl;



import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import com.cmccsi.entity.qxgroupDto;
import com.cmccsi.mapper.qxgroupDtoMapper;
import com.cmccsi.service.GroupService;
import com.cmccsi.utils.MybatisUtil;

public class GroupServiceImpl implements GroupService {
	
	private SqlSessionFactory sessionFactory = MybatisUtil.getInstance();
	private Logger loger = Logger.getLogger(DomainServiceImpl.class);
	@Override
	public Boolean delete(Long id) {
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupDtoMapper grpMapper = session.getMapper(qxgroupDtoMapper.class);
			grpMapper.deleteByPrimaryKey(id);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public Boolean update(qxgroupDto grp) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {			
			qxgroupDtoMapper grpMapper = session.getMapper(qxgroupDtoMapper.class);
			grpMapper.updateByPrimaryKey(grp);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);

			loger.error("出错的Group的name："+grp.getName());
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public qxgroupDto find(Long grpId) {
		// TODO Auto-generated method stub
		qxgroupDto grp = null;
		SqlSession session = sessionFactory.openSession();
		try {
			qxgroupDtoMapper grpMapper = session.getMapper(qxgroupDtoMapper.class);
			grp =grpMapper.selectByPrimaryKey(grpId);
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
		} finally {
			session.close();
		}
		return grp;
	}


	@Override
	public Boolean save(qxgroupDto grp) {
		// TODO Auto-generated method stub
		SqlSession session = sessionFactory.openSession();
		try {			
			qxgroupDtoMapper grpMapper = session.getMapper(qxgroupDtoMapper.class);
			grpMapper.insert(grp);
			session.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			loger.error(e.getMessage(), e);
			loger.error("出错的Group的nam是："+grp.getName());
		} finally {
			session.close();
		}
		return false;
	}

}
